package cn.itsource.service.impl;

import cn.itsource.domain.Param;
import cn.itsource.mapper.ParamMapper;
import cn.itsource.query.ParamQuery;
import cn.itsource.service.ParamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：博客参数Param
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class ParamServiceImpl implements ParamService {

    @Autowired
    private ParamMapper paramMapper;

    @Override
    public List<Param> findAll() {
        return paramMapper.findAll();
    }

    @Override
    public Param findOne(Long id) {
        return paramMapper.findOne(id);
    }

    @Transactional
    @Override
    public void add(Param param) { //
        paramMapper.add(param);
    }

    @Transactional
    @Override
    public void update(Param param) {
        param.setUpdateTime(new Date());
        paramMapper.update(param);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        paramMapper.delete(id);
    }

    @Override
    public PageInfo<Param> pageQuery(ParamQuery query) {
       /* //查询总条数 - 原生分页的写法
        Integer totals = paramMapper.queryCount();
        //查询当前页的分页数据
        List<Param> list = paramMapper.queryData(query);*/
        //开启分页：1.首先查询总数量 2.根据驱动名使用相应的方式进行分页查询，如果是mysql，就会用limit 3.自动计算limit的第1个参数
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Param> list = paramMapper.pageQuery(query);
        return new PageInfo<>(list);
    }

    @Override
    public void patchDel(Long[] ids) {
        paramMapper.patchDel(ids);
    }

    @Override
    public Map<String, String> getParams() {
        Map<String, String>  map = new HashMap<>();
        List<Param> params = paramMapper.findAll();
        for (Param param : params) {
            map.put(param.getParamKey(),param.getParamValue());
        }
        return map;
    }

}
